package com.exam.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ApiResult;
import com.exam.entity.ExamManage;
import com.exam.mapper.ExamManageMapper;
import com.exam.service.ExamManageService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExamManageServiceImpl implements ExamManageService {
    @Resource
    private ExamManageMapper examManageMapper;


    /**
     * 不分页查询所有试卷
     * @return
     */
    @Override
    public ApiResult findAll() {
        List<ExamManage> all = examManageMapper.findAll();
        if (all == null) {
            return ApiResultHandler.buildApiResult(500,"未查询到数据",all);
        }
        return ApiResultHandler.buildApiResult(200, "请求成功！",all );
    }

    /**
     * 分页查询所有试卷
     * @param page 当前页
     * @param size 每页多少条
     * @return
     */
    @Override
    public ApiResult findAll(Integer page,Integer size) {
        Page<ExamManage> examManage = new Page<>(page,size);
        IPage<ExamManage> all = examManageMapper.findAll(examManage);
        if (all == null) {
            return ApiResultHandler.buildApiResult(500,"未查询到数据",all);
        }
        return ApiResultHandler.buildApiResult(200, "请求成功！", all);
    }

    /**
     * 根据考试ID查找当前考试的信息
     * @param examCode 考生的id
     * @return
     */
    @Override
    public ApiResult findById(Integer examCode) {
        ExamManage res  = examManageMapper.findById(examCode);
        if(res == null) {
            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"请求成功！",res);
    }

    /**
     * 根据考试ID删除当前考试的信息
     * @param examCode
     * @return
     */
    @Override
    public ApiResult delete(Integer examCode) {
        int delete = examManageMapper.delete(examCode);
        if(delete<0) {
            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"删除成功",delete);
    }

    /**
     * 修改当前考试信息
     * @param exammanage
     * @return
     */
    @Override
    public ApiResult update(ExamManage exammanage) {
        int update = examManageMapper.update(exammanage);
        if(update<0) {
            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"删除成功",update);
    }

    /**
     * 添加考试信息
     * @param exammanage
     * @return
     */
    @Override
    public ApiResult add(ExamManage exammanage) {
        int add = examManageMapper.add(exammanage);
        if (add <0) {
            return  ApiResultHandler.buildApiResult(400,"添加失败",add);
        }
            return ApiResultHandler.buildApiResult(200, "添加成功", add);

    }

    /**
     * 查询最后一条记录的paperId,返回给前端达到自增效果
     * @return
     */
    @Override
    public ApiResult findOnlyPaperId() {
           ExamManage res = examManageMapper.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",res);
    }
}
